import { type BrowserWindowConstructorOptions, app } from 'electron'
import createWindow from '../common/newWindow'
import services from './services'
import { generateWindowUuid } from '../../utils'

/**
 * 创建系统设置窗口
 *
 * 有需要子窗口显示任务栏图标就不要绑定父级窗口
 *
 * @param parentWindow 父级窗口
 */
const openTrayMenu = async <T extends BrowserWindowConstructorOptions & { x: number; y: number }>(
  config: T
) => {
  let mainWindow = await createWindow('#/tray-menu', <T>{
    title: '屏幕截图',
    width: 130,
    height: 200,
    ...config,
    x: config.x - 110,
    y: config.y - 180
  })

  mainWindow.setSkipTaskbar(true)

  mainWindow.setAppDetails({
    appId: generateWindowUuid()
  })

  mainWindow.on('blur', () => {
    mainWindow.close()
    mainWindow = null as unknown as Electron.BrowserWindow
  })

  services({ mainWindow, app })
}

export default openTrayMenu
